package doip.testcase.multiCommTest.tcp;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class MultiThreadDoipClientTest {
    static Logger LOGGER = LogManager.getLogger(MultiThreadDoipClientTest.class);
    static int threadCount = 30;
    static int reqCount = 1000000;
    static String serverAddr = "tcp://127.0.0.1:21042";
    static Executor executor = Executors.newFixedThreadPool(threadCount);

    public static void main(String[] args) {
        if (args.length > 0) {
            serverAddr = args[0];
            threadCount = Integer.valueOf(args[1]);
            reqCount = Integer.valueOf(args[2]);
        }
        new MultiThreadDoipClientTest().run();
    }

    @Test
    public void run() {
        TCPTestClient.TestResult total = new TCPTestClient.TestResult();
        AtomicInteger counter = new AtomicInteger(0);
        AtomicInteger totalRps = new AtomicInteger(0);
        for (int i = 0; i < threadCount; i++)
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    TCPTestClient test = new TCPTestClient();
                    try {
                        TCPTestClient.TestResult result = test.testRetrive(test.serviceAddr, reqCount, "bdware.test/small");
                        totalRps.addAndGet((int) result.rps);
                    } catch (InterruptedException e) {

                    }
                    counter.getAndIncrement();

                }
            });
        for (; counter.get() < threadCount; ) {
            Thread.yield();
        }
        LOGGER.info("totalRps:" + totalRps.get());

    }
}
